#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <pthread.h>

#include "IODrive.h"
#include "safequeue.h"
#include "user.h"
IODrive *allChannel;

void *thread_function(void *arg)
{
    MatVector *matVector;
    int tid = *(int *)arg;
    printf("new thread start id: %d\n",tid);
    while(1)
    {
        matVector = frame_que.wait_and_pop();
        #if(CAMER_NUM == 1)
        pic_dispose(matVector->frame, tid);
        #elif(CAMER_NUM == 2)
        pic_dispose_2(matVector->frame_1, matVector->frame_2, tid);
        #endif
        matVector->isusing = false;
        waitKey(1);
    }
}

int threadpoolInit(int thread_num)
{
    int err = 0;
    pthread_t ntid;
    if(thread_num < 1)
    {
        thread_num = 1;
    }
    for(int i = 0;i < thread_num;i++)
    {
        int *thread_id = new int(i);
        err = pthread_create(&ntid, NULL, thread_function, thread_id);
        if (err != 0)
        {
            fprintf(stderr, "can't create thread: %s\n", strerror(err));
            return -1;
        }
    }
}

int main()
{

    #if(CAMER_NUM == 1)
        printf("当前模式为单目　\n");
    #elif(CAMER_NUM == 2)
        printf("当前模式为双目　\n");
    #endif
    if(!userInit())
    {
      printf("user init falid\n");
    }
    allChannel = new IODrive;


    threadpoolInit(2);       //设置数据处理线程个数，范围在１～3之间（对于４线程机器而言）
    allChannel->getVidio(25);//设置图像采集时间间隔 此值仅与曝光时间有关。建议值：1000/fps-5
                             //最小值为1000/fps_max+5 fps_max为摄像头最高帧率
    allChannel->showFps();   //使能帧率打印，不需要打印帧率时注释掉


    #ifdef USING_UART2
    allChannel->Uart2->setRecvCallback(uart2Handler);//设置串口2数据处理回调函数
    allChannel->Uart2->enableRecive();//使能接收，不需要接受时可以关闭
    #endif

    #ifdef USING_UART3
    allChannel->Uart3->setRecvCallback(uart3Handler);
    allChannel->Uart3->enableRecive();
    #endif

    allChannel->start();

    return 0;
}



